package com.chaowen.xuefeng;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.chaowen.xuefeng.index.Index_Message;
import com.chaowen.yixin.R;

/**
 * 首页
 * 
 * @author lxj
 * 
 */
public class Fragment_index extends OAFragment implements AdapterView.OnItemClickListener {
	private GridView mGridView;
	private String IMAGE_ITEM = "imgage_item";
	private String TEXT_ITEM = "text_item";
	private String[] arrText = new String[] { "消息中心", "通讯录", "客户资源", "工作日报", "移动办公", "电子签到", "产品展示" };
	private int[] arrImages = new int[] { R.drawable.oa_index_1, R.drawable.oa_index_2, R.drawable.oa_index_3, R.drawable.oa_index_4, R.drawable.oa_index_5, R.drawable.oa_index_6, R.drawable.oa_index_7 };
	SimpleAdapter saImageItems;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_oa_index, container, false);

		mGridView = (GridView) view.findViewById(R.id.gridview);
		saImageItems = new SimpleAdapter(this.getActivity().getApplicationContext(), getGridViewData(), R.layout.fragment_oa_index_grid_view_item, new String[] { IMAGE_ITEM, TEXT_ITEM }, new int[] { R.id.itemImage, R.id.itemText });
		// 设置GridView的adapter。GridView继承于AbsListView。
		mGridView.setAdapter(saImageItems);
		mGridView.setOnItemClickListener(this);

		return view;
	}

	/**
	 * GridView的点击回调函数
	 * 
	 * @param adapter
	 *            -- GridView对应的dapterView
	 * @param view
	 *            -- AdapterView中被点击的视图(它是由adapter提供的一个视图)。
	 * @param position
	 *            -- 视图在adapter中的位置。
	 * @param rowid
	 *            -- 被点击元素的行id。
	 */
	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position, long rowid) {

		// 根据元素位置获取对应的值
		HashMap<String, Object> item = (HashMap<String, Object>) adapter.getItemAtPosition(position);

		String itemText = (String) item.get(TEXT_ITEM);
		Object object = item.get(IMAGE_ITEM);
		//Toast.makeText(this.getActivity().getApplicationContext(), "You Select " + itemText, Toast.LENGTH_SHORT).show();
		intoContent(position, itemText);
	}

	private void intoContent(int position, String title) {
		Intent intent = null;
		
		
		intent = new Intent(this.getActivity().getApplicationContext(), Index_Message.class);
		Bundle bundle = new Bundle();
		bundle.putString("title", title);
		intent.putExtras(bundle);
		startActivity(intent);

		switch (position) {
		case 0:

			break;

		default:
			break;
		}
	}

	/**
	 * 获取GridView的数据
	 */
	private List<HashMap<String, Object>> getGridViewData() {
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

		for (int i = 0; i < 7; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put(IMAGE_ITEM, arrImages[i]);
			map.put(TEXT_ITEM, arrText[i]);
			list.add(map);
		}

		return list;
	}

	@Override
	public String getTitle() {
		return "首页";
	}

}
